﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PSTaskDialog;

namespace smx
{
    public partial class compEdit : Form
    {
        private long selectedid = 0;
        private long supplierid = 0;
        //edit form constructor
        public compEdit(string id)
        {
            InitializeComponent();
            selectedid = Convert.ToInt64(id);
        }
        //add form constructor
        public compEdit()
        {
            InitializeComponent();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //form load event, for edit form this sets the information to selected values
        //----------------------------------------------------------------------------------------------------------------------------
        private void compEdit_Load(object sender, EventArgs e)
        {
            getSupplier();
            if (selectedid != 0)
            {
                try
                {
                    stockmanagerexpressEntities data = new stockmanagerexpressEntities();

                    var q = from c in data.components
                            where c.id == selectedid
                            select c;
                    foreach (component c in q)
                    {
                        idTxt.Text = c.id.ToString();
                        nameTxt.Text = c.name;
                        priceTxt.Text = c.value.ToString();
                        supplierid = c.supplier_id;
                        qtyinstockTxt.Text = c.qtyinstock.ToString();
                        lowlevelTxt.Text = c.lowlevel.ToString();
                        notesTxt.Text = c.notes;
                        fullnameTxt.Text = c.name;
                    }
                    getSpecificSupplier();
                }
                catch (Exception err)
                {
                    cTaskDialog.MessageBox(this, "Error in Code",
                       "There was an error!",
                       "There was an error in attempting to get the record, there could be many reasons. IF the problem persists please let us know, for more information view the details" + err.Message,
                       err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);
                
                }
            }
            
        } 
        
        
        //----------------------------------------------------------------------------------------------------------------------------
        //cancel the form if the confirmation comes back true
        //----------------------------------------------------------------------------------------------------------------------------
        private void CancelBtn_Click(object sender, EventArgs e)
        {
            if (
                       cTaskDialog.MessageBox(this,
                           "Confirmation",
                           "Are you sure you wish to cancel?",
                           "Choosing yes will close this form and no data will be saved. Do you wish to proceed?",
                           eTaskDialogButtons.YesNo,
                           eSysIcons.Question) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //on save check if this is an add or edit form
        //----------------------------------------------------------------------------------------------------------------------------
        private void saveBtn_Click(object sender, EventArgs e)
        {
            if (selectedid == 0)
                newREcord();
            else
                EditRecord();



            //go back to master form and refresh the grid
            MasterForm form = (MasterForm)Application.OpenForms["MasterForm"];
            form.getComps();
            this.Close();
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //if add form insert function
        //----------------------------------------------------------------------------------------------------------------------------
        private void newREcord()
        {
            stockmanagerexpressEntities data = new stockmanagerexpressEntities();

            try
            {
                keyGen g = new keyGen();
                long newId = g.generateId();
                component c = new component();
                c.id = newId;
                c.name = nameTxt.Text;
                c.value = Convert.ToDecimal(priceTxt.Text);
                c.qtyinstock = Convert.ToInt32(qtyinstockTxt.Text);
                c.lowlevel = Convert.ToInt32(lowlevelTxt.Text);
                c.notes = notesTxt.Text;
                c.supplier_id = (long)companyCombo.SelectedValue;
                data.AddTocomponents(c);
                data.SaveChanges();

            }
            catch (Exception err)
            {
                cTaskDialog.MessageBox(this, "Error in Code",
                                               "There was an error!",
                                               "There was an error in attempting to add the record, there could be many reasons. IF the problem persists please let us know, for more information view the details" + err.Message,
                                               err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);
            }
        }


        //----------------------------------------------------------------------------------------------------------------------------
        //if edit form save function
        //----------------------------------------------------------------------------------------------------------------------------
        private void EditRecord()
        {
            stockmanagerexpressEntities data = new stockmanagerexpressEntities();

            var q = from c in data.components
                    where c.id == selectedid
                    select c;
            try
            {
                foreach (component c in q)
                {
                    c.name = nameTxt.Text;
                    c.value = Convert.ToDecimal(priceTxt.Text);
                    c.qtyinstock = Convert.ToInt32(qtyinstockTxt.Text);
                    c.lowlevel = Convert.ToInt32(lowlevelTxt.Text);
                    c.notes = notesTxt.Text;
                    c.supplier_id = (long)companyCombo.SelectedValue;
                    data.SaveChanges();
                }
            }
            catch (Exception err)
            {
                cTaskDialog.MessageBox(this, "Error in Code",
                "There was an error!",
                "There was an error in attempting to edit the record, there could be many reasons. IF the problem persists please let us know, for more information view the details" + err.Message,
                err.ToString(), "Database error", "", eTaskDialogButtons.OK, eSysIcons.Error, eSysIcons.Error);
            }
        }


        //----------------------------------------------------------------------------------------------------------------------------
        //get the supplier information
        //----------------------------------------------------------------------------------------------------------------------------
        private void getSupplier()
        {
            stockmanagerexpressEntities data = new stockmanagerexpressEntities();

            var combo = from c in data.suppliers
                        select new
                        {
                            c.id,
                            c.company
                        };
            companyCombo.DisplayMember = "company";
            companyCombo.ValueMember = "id";
            companyCombo.DataSource = combo;
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //gets the information from the selected person
        //----------------------------------------------------------------------------------------------------------------------------
        private void companyCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            stockmanagerexpressEntities data = new stockmanagerexpressEntities();

            int sid = Convert.ToInt32(companyCombo.SelectedValue);

            var supp = from s in data.suppliers
                       where s.id == sid
                       select s;
            foreach (supplier s in supp)
            {
                suppfirstnameTxt.Text = s.firstname;
                supplastname.Text = s.lastname;
            }
        }

        //----------------------------------------------------------------------------------------------------------------------------
        //gets the supplier for this record if not new
        //----------------------------------------------------------------------------------------------------------------------------
        private void getSpecificSupplier()
        {
            stockmanagerexpressEntities data = new stockmanagerexpressEntities();

            var q = (from c in data.components
                    where c.id == selectedid
                    select c.supplier_id).FirstOrDefault();

            var supp = (from s in data.suppliers
                        where s.id == q
                        select s);

            foreach (supplier s in supp)
            {
                companyCombo.SelectedValue = s.id;
                companyCombo.Text = s.company;
            }


        }


    }
}
